package com.stn.zjx.givingtreasure.activity.msg;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.came.viewbguilib.ButtonBgUi;
import com.google.gson.Gson;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.header.progresslayout.ProgressLayout;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.adapter.MsgItemListAdapter;
import com.stn.zjx.givingtreasure.base.BaseActivity;
import com.stn.zjx.givingtreasure.domain.MsgListItemDomain;
import com.stn.zjx.givingtreasure.event.NetWorkEvent;
import com.stn.zjx.givingtreasure.net.BaseApi;
import com.stn.zjx.givingtreasure.net.impl.MsgNetImpl;
import com.stn.zjx.givingtreasure.service.UserService;

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MsgListItemActivity extends BaseActivity {
    @BindView(R.id.back_bt)
    RelativeLayout backBt;
    @BindView(R.id.action_title)
    TextView actionTitle;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.twinkling_refresh)
    TwinklingRefreshLayout twinklingRefresh;


    @BindArray(value = R.array.msg_list)
    public String[] msg_arr;


    int pagenow = 1;
    int pagesize = 10;
    @BindView(R.id.action_add_bt)
    RelativeLayout actionAddBt;
    @BindView(R.id.web_title)
    TextView webTitle;
    @BindView(R.id.action_refresh)
    RelativeLayout actionRefresh;
    @BindView(R.id.exit)
    RelativeLayout exit;
    @BindView(R.id.sure_bt)
    TextView sureBt;
    @BindView(R.id.share_bt)
    RelativeLayout shareBt;
    @BindView(R.id.release_bt)
    ButtonBgUi releaseBt;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    private boolean isMoreLoading = false;
    private int visibleItemCount = 0;
    private LinearLayoutManager lm;
    private MsgItemListAdapter adapter;
    private String msgTitle = "";
    private Integer msgType = 0;

    @Override
    public void networkStatus(NetWorkEvent netWorkEvent) {

    }


    @Override
    public void initComponent() {
        msgTitle = getIntent().getStringExtra("msgTitle");
        msgType = getIntent().getIntExtra("msgType", 0);
        actionTitle.setText(msgTitle);
        backBt.setVisibility(View.VISIBLE);


        ProgressLayout headerView = new ProgressLayout(this);
        headerView.setColorSchemeResources(R.color.bg_red_orange, R.color.bg_red_orange1);
        twinklingRefresh.setHeaderView(headerView);
        rv.setLayoutManager(lm = new LinearLayoutManager(this, OrientationHelper.VERTICAL, false));
        rv.setAdapter(adapter = new MsgItemListAdapter(this));
        rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (!isMoreLoading && newState == RecyclerView.SCROLL_STATE_IDLE && visibleItemCount == adapter.dataBeanList.size() - 1 &&
                        adapter.dataBeanList.size() >= pagenow * 10) {
                    isMoreLoading = true;
                    initAppend();
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                visibleItemCount = lm.findLastVisibleItemPosition();
            }
        });
        twinklingRefresh.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(final TwinklingRefreshLayout refreshLayout) {
                initRefresh();
            }

            @Override
            public void onLoadMore(final TwinklingRefreshLayout refreshLayout) {
                initAppend();
            }
        });

        twinklingRefresh.startRefresh();
        //初始化popu弹窗


    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_msg_type_list);
        ButterKnife.bind(this);
        initComponent();
    }

    @OnClick({R.id.back_bt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_bt:
                finish();
                break;


        }
    }

    public void initRefresh() {
        if (UserService.getInstance(this).getOnlineLoginUser() == null) {
            twinklingRefresh.finishRefreshing();
            return;
        }
        MsgNetImpl.query_get_msg_list(pagenow, pagesize, msgType,
                UserService.getInstance(this).getOnlineLoginUser().getToken(),
                UserService.getInstance(this).getOnlineLoginUser().getUserId() + "_query_get_msg_list_" + msgType,
                new StringCallback() {
                    @Override
                    public void onFinish() {
                        super.onFinish();
                        twinklingRefresh.finishRefreshing();
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        if(response.isSuccessful()&&!isFinishing()){
                            Gson gson = new Gson();
                            MsgListItemDomain domain = gson.fromJson(response.body(), MsgListItemDomain.class);
                            switch (domain.getStatus()) {
                                case BaseApi.REQUEST_SUCCESS_CODE:
                                    if (domain.getData() != null && domain.getData().size() != 0) {
                                        noDataTv.setVisibility(View.GONE);
                                        adapter.refreshList(domain.getData());
                                    }else{
                                        noDataTv.setVisibility(View.VISIBLE);
                                        adapter.clearList();
                                    }
                                    break;
                                default:
                                    noDataTv.setVisibility(View.VISIBLE);
                                    adapter.clearList();
                                    break;
                            }
                        }

                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        if(response.isSuccessful()&&!isFinishing()){

                            Gson gson = new Gson();
                            MsgListItemDomain domain = gson.fromJson(response.body(), MsgListItemDomain.class);
                            switch (domain.getStatus()) {
                                case BaseApi.REQUEST_SUCCESS_CODE:
                                    if (domain.getData() != null && domain.getData().size() != 0) {
                                        noDataTv.setVisibility(View.GONE);
                                        adapter.refreshList(domain.getData());
                                    }else{
                                        noDataTv.setVisibility(View.VISIBLE);
                                        adapter.clearList();
                                    }
                                    break;
                                default:
                                    noDataTv.setVisibility(View.VISIBLE);
                                    adapter.clearList();
                                    break;
                            }

                        }

                    }
                });

    }

    public void initAppend() {
        if (UserService.getInstance(this).getOnlineLoginUser() == null) {
            twinklingRefresh.finishRefreshing();
            return;
        }
        pagenow++;
        MsgNetImpl.query_get_msg_list(pagenow, pagesize, msgType,
                UserService.getInstance(this).getOnlineLoginUser().getToken(),
                UserService.getInstance(this).getOnlineLoginUser().getUserId() + "_query_get_msg_list_" + msgType,
                new StringCallback() {
                    @Override
                    public void onFinish() {
                        super.onFinish();
                        twinklingRefresh.finishRefreshing();
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        MsgListItemDomain domain = gson.fromJson(response.body(), MsgListItemDomain.class);
                        switch (domain.getStatus()) {
                            case BaseApi.REQUEST_SUCCESS_CODE:
                                if (domain.getData() != null && domain.getData().size() != 0) {
                                    adapter.appendList(domain.getData());
                                }
                                break;
                        }
                    }
                });

    }

}
